package com.Example;

import java.util.ArrayList;
import java.util.Random;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.scenes.scene2d.ui.List;

public class Turret extends BaseBoxObject {
	private static final float WIDTH = 0;
	private static final float HEIGHT = 0;
	private static final float RADIUS = 0;
	private static final float DENSITY = 0;
	private static final float RESTITUTION = 0;
	boolean direction = true;
	public ArrayList<Bullet> bullets = new ArrayList<Bullet>();
	private Vector2 pos;
	TextureWrapper textureNozzle;
	TextureWrapper textureBase;
	private float rotation;
	public float lastStateTime = 0;
	private float stateTime = 0;
	Random rand;
	
	
	Vector3 campos;

	public Turret(int bodyIndex, int collisionGroup, float px, float py,
			float angle , Vector3  cameraPos) {
		
		super(bodyIndex, collisionGroup);
		this.pos = new Vector2(px, py);
		BodyType bodyType = BodyType.StaticBody;
		this.MakeBody(WIDTH, HEIGHT, RADIUS, bodyType, DENSITY, RESTITUTION,
				pos, angle);
		this.textureBase = new TextureWrapper(Assets.turretBase, pos);
		Vector2 posNozzle = new Vector2(pos.x, pos.y + 35);

		this.textureNozzle = new TextureWrapper(Assets.turretNozzle, posNozzle);
		this.textureNozzle.SetOrigin(15, 0);
		rand = new Random();
		rotation = rand.nextInt(140)-70 ;
		if (rand.nextFloat() > 0.5f) {
			direction = false;
		}
		this.campos=cameraPos;
	}
	

	@Override
	public void Draw(SpriteBatch sp) {
		// TODO Auto-generated method stub
		if (1 == 1) {
			textureNozzle.Draw(sp);
			textureBase.Draw(sp);
			for (int i = 0; i < this.bullets.size(); i++) {
				this.bullets.get(i).Draw(sp);
			}
		}
	}

	public void SetTextureDimension(int width, int height) {
		// textureNozzle.SetDimension(width, height);
		// textureBase.SetDimension(width, height);
	}

	public void Update(float deltaTime) {
	////////////////////////////  TODO
		if (true) {
			super.Update(deltaTime);
			if (direction) {
				rotation =  (rotation + (float) 10.0 * deltaTime);

			} else {
				rotation =  (rotation - (float) 10.0 * deltaTime);

			}

			if (rotation <= -70) {
				direction = true;
			}
			if (rotation >= 70) {
				direction = false;
			}
			this.textureNozzle.SetRotation((int)rotation);
			this.lastStateTime += deltaTime;
			if (this.isInScreen() == true) {
				// TODO we need to fix the rate of fire ----------------------asa-----------
				if (lastStateTime > rand.nextFloat() * 2 + .5) {
					///  send bullet from end of nozzle
					Bullet b = new Bullet(0, GlobalSettings.ENEMY_GROUP, pos.x+textureNozzle.GetHeight()	* (float) Math.sin(rotation*3.1415/180)*-1
							, pos.y + textureNozzle.GetHeight()	* (float) Math.cos(rotation*3.1415/180),
							this.rotation);
					b.SetTextureDimension(TextureDimensions.BALL_WIDTH / 2,
							TextureDimensions.BALL_HEIGHT / 2);
					bullets.add(b);
					lastStateTime = 0;

				}
			}
			for (int i = 0; i < bullets.size(); i++) {
				this.bullets.get(i).Update(deltaTime);

				if (bullets.get(i).GetPosition().y >= campos.y+ GlobalSettings.VIRTUAL_HEIGHT/2-10
						|| bullets.get(i).GetPosition().x >= campos.x+GlobalSettings.VIRTUAL_WIDTH/2-10
						|| bullets.get(i).GetPosition().x <= campos.x-GlobalSettings.VIRTUAL_WIDTH/2+10
						
						) {
					bullets.get(i).boxUserData.setDead(true);
				}
				if (bullets.get(i).boxUserData.isDead()==true)
				{
					bullets.get(i).DestroyBody();
					bullets.remove(i);
					
				}
			}

			stateTime += deltaTime;
		}

	}

	private boolean isInScreen() {
		if (this.GetBoxPosition().x - BoxPigeon.getPosition().x < GlobalSettings.VIRTUAL_WIDTH) {
			return true;

		}
		return false;
	}

}
